package com.oocl.crm;

import java.awt.HeadlessException;
import java.security.AllPermission;

import com.oocl.crm.DoubleLinkList.Node;

public class DLLControl {

	//删除链表dll中的person节点
	public static void deleteNode(DoubleLinkList<Person> dll,DoubleLinkList<Person>.Node<Person> node)
	{
		DoubleLinkList<Person>.Node<Person> p=node;
		if(p.next==null)
		{
			p.prev.next=null;
			p=null;
			dll.reduceSize();
		}
		else
		{
			p.prev.next=p.next;
			p.next.prev=p.prev;
			p=null;
			dll.reduceSize();
		}
	}
	//打印某节点
	public static void printNode(DoubleLinkList<Person>.Node<Person> node)
	{
		Person person;
		person = node.getData();
		System.out.println("id:"+person.id+" name:"+person.name+" sex:"+person.sex+" birthday:"+person.birthday+" address:"+person.address+" call:"+person.call);
	}
	//打印整个链表
	public static void printList(DoubleLinkList<Person> dll)
	{
		DoubleLinkList<Person>.Node<Person> p;
		p = dll.getHead();
		while(p.next!=null)
		{
			DLLControl.printNode(p.next);
			p=p.next;
		}
		System.out.println();
	}
	//按id查找某节点
	public static DoubleLinkList<Person>.Node<Person> searchById(DoubleLinkList<Person> dll,String id)
	{
		
		DoubleLinkList<Person>.Node<Person> p =dll.getHead();
		while(p.next!=null)
		{
			if(p.next.getData().id.compareTo(id)==0)
			{
				return p.next;
			}
			p=p.next;
		}
		return null;
	}
	//比较某节点name值是否与n相等
	public static boolean compareByName(DoubleLinkList<Person>.Node<Person> comp,String n)
	{
		DoubleLinkList<Person>.Node<Person> p=comp;
		if(p.getData().name.compareTo(n)==0)
		{
			return true;
		}
		else return false;
	}
	//比较某节点sex值是否与n相等
	public static boolean compareBySex(DoubleLinkList<Person>.Node<Person> comp,String n)
	{
		DoubleLinkList<Person>.Node<Person> p=comp;
		if(p.getData().sex.compareTo(n)==0)
		{
			return true;
		}
		else return false;
	}
	//比较某节点address值是否与n相等
	public static boolean compareByAddress(DoubleLinkList<Person>.Node<Person> comp,String n)
	{
		DoubleLinkList<Person>.Node<Person> p=comp;
		if(p.getData().address.compareTo(n)==0)
		{
			return true;
		}
		else return false;
	}
	//比较某节点call值是否与n相等
	public static boolean compareByCall(DoubleLinkList<Person>.Node<Person> comp,String n)
	{
		DoubleLinkList<Person>.Node<Person> p=comp;
		if(p.getData().call.compareTo(n)==0)
		{
			return true;
		}
		else return false;
	}
	//比较某节点birthday值是否与birth相等
	public static boolean compareByBirthday(DoubleLinkList<Person>.Node<Person> comp,String n)
	{
		DoubleLinkList<Person>.Node<Person> p=comp;
		if(p.getData().birthday.compareTo(n)==0)
		{
			return true;
		}
		else return false;
	}
	//交换相邻节点a和b在链表中的位置
	public static void exchangeNode(DoubleLinkList<Person>.Node<Person> a,DoubleLinkList<Person>.Node<Person> b)
	{
		a.prev.next=b;
		if(b.next!=null)b.next.prev=a;
		b.prev=a.prev;
		a.next=b.next;
		a.prev=b;
		b.next=a;
	}
    //冒泡排序按姓名
	public static void bubbleSortById(DoubleLinkList<Person> dll)
	{
		int i;
		int test;
		DoubleLinkList<Person>.Node<Person> node1,node2;
		for(i=0;i<(dll.getSize()-1);i++)
		{	
			node1=dll.getHead();
			while(node1.next!=null&&node1.next.next!=null)
			{
				if((test=node1.next.getData().id.compareTo(node1.next.next.getData().id))>0)
				{
//					System.out.print(test);
					node2=node1.getNext();
					DLLControl.exchangeNode(node1.next,node2.next);
				}
				node1=node1.next;
			}
			
		}
	}
////	
}



